package mysql

import (
	"gorm.io/gorm"
	"novel-sys/goods/goods"
)

type Novel struct {
	gorm.Model
	Title    string `gorm:"type:varchar(50)"`
	Category string `gorm:"type:varchar(50)"`
	Label    string `gorm:"type:varchar(50)"`
	Info     string `gorm:"type:varchar(50)"`
	UserId   int64  `gorm:"type:int(11)"`
}

func (n *Novel) NovelAdd(db *gorm.DB) error {
	return db.Debug().Create(n).Error
}

func (n *Novel) NoveBycomment(db *gorm.DB, id int64) goods.CommentListResponse {
	var data goods.CommentListResponse
	db.Debug().Model(&Novel{}).
		Select(
			"novels.*",
			"users.head_image",
			"users.nick_name").
		Joins("LEFT JOIN users ON novels.user_id=users.id").
		Order("novels.created_at").
		Where("novels.id=?", id).
		Find(&data)
	return data
}
func NovelList(db *gorm.DB, in *goods.NovelListRequest) []*goods.NovelList {
	var data []*goods.NovelList
	db.Debug().Model(&Novel{}).
		Select(
			"novels.*",
			"users.head_image",
			"users.nick_name").
		Joins("LEFT JOIN users ON novels.user_id=users.id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Order("novels.created_at").
		Find(&data)
	return data
}
func NovelList2(db *gorm.DB, in *goods.NovelList2Request) ([]*goods.NovelList2, int64) {
	var data []*goods.NovelList2
	var total int64
	tx := db.Debug().Model(&Novel{}).
		Select(
			"novels.*",
			"users.head_image",
			"users.nick_name").
		Joins("LEFT JOIN users ON novels.user_id=users.id")
	if in.Title != "" {
		tx.Where("novels.title like ?", "%"+in.Title+"%")

	}
	tx.Count(&total)
	tx.Scopes(Paginate(int(in.Page), int(in.Size))).Find(&data)
	return data, total
}
